package com.siyoumi.app.modules.user.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.siyoumi.app.entity.*;
import com.siyoumi.app.modules.user.entity.EnumCorpPayState;
import com.siyoumi.app.modules.user.service.SvcSysSale;
import com.siyoumi.app.modules.user.vo.VoSaleGetMoney;
import com.siyoumi.app.service.SysSaleClientService;
import com.siyoumi.app.service.SysSaleRecordService;
import com.siyoumi.component.XBean;
import com.siyoumi.component.XEnumBase;
import com.siyoumi.component.http.InputData;
import com.siyoumi.controller.WxAppApiController;
import com.siyoumi.mybatispuls.JoinWrapperPlus;
import com.siyoumi.util.XReturn;
import com.siyoumi.validator.XValidator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/wxapp/user/api_sale")
public class ApiSale
        extends WxAppApiController {
    /**
     * 粉丝列表
     */
    @GetMapping("client_list")
    public XReturn clientList() {
        InputData inputData = InputData.fromRequest();

        String[] select = {
                "sacl_id",
                "sacl_create_date",
                "user_id",
                "user_name",
                "user_headimg",
        };
        JoinWrapperPlus<SysSaleClient> query = SvcSysSale.getBean().listClientQuery(inputData);
        query.join(SysUser.table(), SysUser.tableKey(), "sacl_client_uid");
        query.select(select);
        query.eq("sacl_sale_uid", getUid());

        IPage<SysSaleClient> page = new Page<>(inputData.getPageIndex(), inputData.getPageSize(), false);
        //list
        IPage<Map<String, Object>> pageData = SysSaleClientService.getBean().getMaps(page, query);
        List<Map<String, Object>> list = pageData.getRecords();

        for (Map<String, Object> item : list) {
            SysSaleClient entity = XBean.fromMap(item, SysSaleClient.class);
            item.put("id", entity.getKey());
        }

        getR().setData("list", list);

        return getR();
    }


    /**
     * 收入明细列表
     */
    @GetMapping("record_list")
    public XReturn recordList() {
        InputData inputData = InputData.fromRequest();

        String[] select = {
                "srec_id",
                "srec_create_date",
                "srec_ab_type",
                "srec_id_src",
                "srec_desc",
                "srec_num",
                "srec_uid",
                "corppay_id",
                "corppayq_status",
                "corppayq_api_status",
                "corppayq_status_date",
        };
        JoinWrapperPlus<SysSaleRecord> query = SvcSysSale.getBean().listRecordQuery(inputData);
        query.leftJoin(SysCorpPay.table(), "corppayq_id_src", "srec_id");
        query.select(select);
        query.eq("srec_uid", getUid());
        query.orderByDesc("srec_create_date")
                .orderByAsc("srec_id");

        IPage<SysSaleRecord> page = new Page<>(inputData.getPageIndex(), inputData.getPageSize());
        IPage<Map<String, Object>> pageData = SysSaleRecordService.getBean().getMaps(page, query);
        List<Map<String, Object>> list = pageData.getRecords();

        EnumCorpPayState enumState = XEnumBase.of(EnumCorpPayState.class); //状态中文翻译
        for (Map<String, Object> item : list) {
            String apiStatus = (String) item.get("corppayq_api_status");
            item.put("corppayq_api_status", apiStatus);
        }

        getR().setData("list", list);

        return getR();
    }
}
